/**
 * SimpleMapControler Class
 * copyrigth 2006-2007 © geolabs
 * GPL-2.0
 * author: gerald@djayux.net
 *
 */
System.require("Effect");
System.require("OctoXml");

SimpleMapControler=Controler.extend();

SimpleMapControler.define({

  _init: function(loading_image,map_image,opacity){
      this.content=document.getElementById(map_image);
      this.loading=document.getElementById(loading_image);
      //this.status=this.loading;
      this.fade=opacity?opacity:95;
    },

  setContent: function(){
      this.content.src=arguments[0];
    },

  setTime: function(){
      this.time.innerHTML=arguments[0];
    },

  resetCMD: function(){
      /**
       * @todo must be a reference to the toolbar instance and not a global 
       * variable as it is right now.
       */
      var tmp=document.myform.OLDCMD.value.split("_");
      tmp=(tmp.length>1)?tmp[0]+""+tmp[1]:tmp[0];
      myToolbar.setActive(tmp.toLowerCase());
    },

  updateKeymap: function(){
      OctoXml.getElementsByAttrAndValue(document,"name","keymap")[0].src=OctoXml.getAttributeValue(OctoXml.getElementsByAttrAndValue(this.request.responseXML,"name","keymap")[0],"src");
    },

  updateForm: function(){
      document.myform.minx.value=OctoXml.getAttributeValue(OctoXml.getElementsByAttrAndValue(this.request.responseXML,"name","minx")[0],"value");
      document.myform.miny.value=OctoXml.getAttributeValue(OctoXml.getElementsByAttrAndValue(this.request.responseXML,"name","miny")[0],"value");
      document.myform.maxx.value=OctoXml.getAttributeValue(OctoXml.getElementsByAttrAndValue(this.request.responseXML,"name","maxx")[0],"value");
      document.myform.maxy.value=OctoXml.getAttributeValue(OctoXml.getElementsByAttrAndValue(this.request.responseXML,"name","maxy")[0],"value");
      document.myform.imagewidth.value=OctoXml.getAttributeValue(OctoXml.getElementsByAttrAndValue(this.request.responseXML,"name","imagewidth")[0],"value");
      document.myform.imageheight.value=OctoXml.getAttributeValue(OctoXml.getElementsByAttrAndValue(this.request.responseXML,"name","imageheight")[0],"value");

      if(System.debug)
	alert("minx:"+document.myform.minx.value+"\n"+
	      "miny:"+document.myform.miny.value+"\n"+
	      "maxx:"+document.myform.maxx.value+"\n"+
	      "maxy:"+document.myform.maxy.value+"\n"+
	      "imagewidth:"+document.myform.imagewidth.value+"\n"+
	      "imageheight:"+document.myform.imageheight.value+"\n");
      
      /**
       * @todo MyAGmap must be referenced from this class' instance !
       * done like this for quicker development...
       */
      MyAGmap.update();
      
      var tmp=Position.fromElement(document.getElementById('mapspace0'));
      Position.ensurePositioned(document.getElementById('MyMapDisplayer0'),tmp);
      Position.ensurePositioned(document.getElementById('MyMapPrepage0'),tmp);
      
      var tmp1=new Position(tmp.x,tmp.y);
      tmp1.y+=(parseInt(document.myform.imageheight.value)-10);
      Position.ensurePositioned(document.getElementById('MyMapXY0'),tmp1);
      document.getElementById('MyMapXY0').style.width=document.myform.imagewidth.value+"px";

      tmp.x+=(parseInt(document.myform.imagewidth.value)-25);
      Position.ensurePositioned(document.getElementById('MyMapToolBar0'),tmp);

    },

  updateQuery: function(){
      var tmp=this.request.responseXML.getElementsByTagName("qr");
      if(!tmp)
	tmp=this.request.responseXML.getElementById("qr");
      // problem in safari for read qr 
      if(tmp.length>0)
	document.getElementById('query_result').innerHTML=OctoXml.serializeToString(tmp[0]);
    },

  updateScaleBar: function(){
      var tmp=OctoXml.getElementsByAttrAndValue(this.request.responseXML,"name","scalebar");
      if(tmp.legth>0)
	document.getElementById('scalebar').src=OctoXml.getAttributeValue(tmp[0],"value");
    },
      
  reinitFade: function(){
      //this.fade=10;
    },

  displayStatus: function(){
      this.superCall("displayStatus",arguments);
      this.loading.style.display="inline";
      Styler.setOpacity(this.loading,this.fade)
    },

  newData: function(){
      try{
	// View update
	this.displayStatus("Displaying");
	var bgim=OctoXml.getAttributeValue(OctoXml.getElementsByAttrAndValue(this.request.responseXML,"name","img_url")[0],"value");
	this.content.style.backgroundImage="url('"+bgim+"')";

	this.updateKeymap();
	//alert("ok keymap");

	this.updateScaleBar();
	//alert("ok scalebar");

	this.updateForm();
	//alert("ok form");

	this.updateQuery();
	//alert("ok query");

	this.request=null;

	/**
	 * @todo MyAGmap must be a property of this class
	 */
	MyAGmap.controler.init(document.getElementById('MyMap0'));
	MyAGmap.controler.initializeValues();

	Styler.setOpacity(this.loading,10);
	Styler.setOpacity(this.content,0);

	Effect.appear({id: this.content.id,start:0,limit:10});
	Effect.fade({id: this.loading.id,start:0,limit:6});
	setTimeout("document.getElementById('"+this.loading.id+"').style.display='none'",2000);

      }
      catch(e) {
	alert("argh : "+e);
	this.status=this.loadgin;
	this.displayStatus("There was an error :\n" +this.request.statusText+" "+e);
      }
    }
  });
